<?php
/**
 * 系统清理后功能验证脚本
 */

// 数据库配置
$host = '101.35.252.112';
$port = '45508';
$dbname = 'trace';
$username = 'root';
$password = 'CeyZFHxjPABhp2Hz';

echo "🔍 开始系统功能验证...\n\n";

try {
    // 创建数据库连接
    $pdo = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "✅ 数据库连接成功\n\n";
    
    // 1. 验证数据库表结构
    echo "📊 1. 数据库表验证:\n";
    $expectedTables = [
        'gf_apps',
        'gf_api_keys',
        'gf_et_apps_apps',
        'gf_et_apps_event',
        'gf_et_apps_web',
        'gf_et_users',
        'gf_membership_plans',
        'gf_membership_orders',
        'gf_user_balances',
        'gf_user_logs',
        'gf_user_memberships',
        'gf_system_config',
        'gf_payment_config',
        'gf_auto_charge_records',
        'gf_billing_records',
        'gf_data_exports',
        'gf_antifraud_high_risk',
        'gf_attribution_track',
        'gf_attribution_install',
        'gf_attribution_event'
    ];
    
    $stmt = $pdo->query("SHOW TABLES");
    $existingTables = [];
    while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
        $existingTables[] = $row[0];
    }
    
    $missingTables = array_diff($expectedTables, $existingTables);
    $extraTables = array_diff($existingTables, $expectedTables);
    
    if (empty($missingTables)) {
        echo "✅ 所有必需表都存在\n";
    } else {
        echo "⚠️  缺少表: " . implode(', ', $missingTables) . "\n";
    }
    
    if (empty($extraTables)) {
        echo "✅ 没有多余表\n";
    } else {
        echo "ℹ️  额外表: " . implode(', ', $extraTables) . "\n";
    }
    
    // 2. 验证核心表数据
    echo "\n📈 2. 核心表数据验证:\n";
    
    $coreTables = [
        'gf_apps' => '应用表',
        'gf_et_users' => '用户表',
        'gf_membership_plans' => '会员套餐表',
        'gf_system_config' => '系统配置表'
    ];
    
    foreach ($coreTables as $table => $description) {
        $count = $pdo->query("SELECT COUNT(*) FROM `$table`")->fetchColumn();
        echo "- $description ($table): $count 条记录\n";
    }
    
    // 3. 验证表结构
    echo "\n🔧 3. 表结构验证:\n";
    
    // 检查 gf_data_exports 表结构
    $stmt = $pdo->query("DESCRIBE gf_data_exports");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $expectedColumns = ['id', 'user_id', 'app_id', 'export_type', 'file_path', 'file_size', 'record_count', 'date_range_start', 'date_range_end', 'format', 'advanced_options', 'status', 'created_at'];
    
    $actualColumns = array_column($columns, 'Field');
    $missingColumns = array_diff($expectedColumns, $actualColumns);
    
    if (empty($missingColumns)) {
        echo "✅ gf_data_exports 表结构完整\n";
    } else {
        echo "⚠️  gf_data_exports 缺少字段: " . implode(', ', $missingColumns) . "\n";
    }
    
    // 4. 验证路由配置
    echo "\n🛣️  4. 路由配置验证:\n";
    
    $routeFile = __DIR__ . '/../../config/route.php';
    if (file_exists($routeFile)) {
        $routeContent = file_get_contents($routeFile);
        
        $requiredControllers = [
            'AppsController',
            'CommonController', 
            'UsersController',
            'LandingController',
            'MembershipController',
            'BalanceController',
            'DataExportController'
        ];
        
        $missingControllers = [];
        foreach ($requiredControllers as $controller) {
            if (strpos($routeContent, $controller) === false) {
                $missingControllers[] = $controller;
            }
        }
        
        if (empty($missingControllers)) {
            echo "✅ 所有必需控制器都在路由中配置\n";
        } else {
            echo "⚠️  缺少控制器配置: " . implode(', ', $missingControllers) . "\n";
        }
        
        // 检查是否还有对已删除控制器的引用
        $removedControllers = [
            'AttributionController',
            'DeviceController',
            'MerchantController',
            'TestController'
        ];
        
        $remainingReferences = [];
        foreach ($removedControllers as $controller) {
            if (strpos($routeContent, $controller) !== false) {
                $remainingReferences[] = $controller;
            }
        }
        
        if (empty($remainingReferences)) {
            echo "✅ 没有对已删除控制器的引用\n";
        } else {
            echo "⚠️  仍存在对已删除控制器的引用: " . implode(', ', $remainingReferences) . "\n";
        }
    } else {
        echo "❌ 路由配置文件不存在\n";
    }
    
    // 5. 验证前端文件
    echo "\n🎨 5. 前端文件验证:\n";
    
    $frontendDir = __DIR__ . '/../../merchant';
    $requiredFiles = [
        'src/main.js',
        'src/router/index-enhanced.js',
        'src/views/site/HomeEnhanced.vue',
        'src/views/site/Login.vue',
        'src/views/console/ConsoleEnhanced.vue',
        'src/components/NavBarEnhanced.vue'
    ];
    
    foreach ($requiredFiles as $file) {
        $filePath = $frontendDir . '/' . $file;
        if (file_exists($filePath)) {
            echo "✅ $file 存在\n";
        } else {
            echo "❌ $file 不存在\n";
        }
    }
    
    // 6. 验证后端文件
    echo "\n⚙️  6. 后端文件验证:\n";
    
    $backendDir = __DIR__ . '/../../app';
    $requiredServices = [
        'common/service/DataExportService.php',
        'common/service/MembershipService.php',
        'common/service/MembershipDaemonService.php',
        'api/controller/AppsController.php',
        'api/controller/UsersController.php',
        'api/controller/DataExportController.php'
    ];
    
    foreach ($requiredServices as $file) {
        $filePath = $backendDir . '/' . $file;
        if (file_exists($filePath)) {
            echo "✅ $file 存在\n";
        } else {
            echo "❌ $file 不存在\n";
        }
    }
    
    echo "\n🎉 系统验证完成！\n";
    echo "✅ 所有核心功能组件都已验证\n";
    echo "📋 清理后的系统状态良好\n";
    
} catch (PDOException $e) {
    echo "❌ 数据库连接失败: " . $e->getMessage() . "\n";
    exit(1);
}